/*******************************************************************************

PooledRegions analysis_CDFs.do

2022.09.02 (DJ) Created

*******************************************************************************/


/*******************************************************************************
	GENERATE CDFs of Travel Cost-Adjusted Prices
*******************************************************************************/	
set scheme s2mono
	
// compile the two data sets
	
	// Kilimanjaro
		use "${analysis}/Kilimanjaro ViltoAV_censusappended.dta", clear
		merge 1:1 village_name ward district using "${analysis}/Kilimanjaro ViltoMaizemarket.dta"
		drop _merge
		merge 1:1 village_name ward district using "${analysis}/Kilimanjaro google_ViltoCities.dta"
		drop _merge
		
		gen survey_region="Kilimanjaro"
		
		tempfile kili_vilaccess
		save `kili_vilaccess'
		
	// Manyara
		use "${analysis}/Manyara ViltoAV_censusappended.dta", clear
		merge 1:1 village_id using "${analysis}/Manyara ViltoMaizemarket.dta"
		drop _merge
		merge 1:1 village_id using "${analysis}/Manyara ViltoCities_dist.dta"
		drop _merge
		
	append using `kili_vilaccess'

		
		
// create travel-adjusted prices in relative terms.

	local inputprice min_DR_adj_price_50kg
	local outputprice max_DR_adj_maize_120kgUSD
	
	foreach var in `inputprice' `outputprice'{
		sum `var', d
		replace `var' = `r(p1)' 	if `var'<`r(p1)' 	& !mi(`var')
		replace `var' = `r(p99)' 	if `var'>`r(p99)' 	& !mi(`var')
	}
		

	sum `inputprice'
	gen travel_adj_urea = (`inputprice'-`r(mean)')/`r(mean)'*100
	
	sum `outputprice'
	gen travel_adj_maize = (`outputprice'-`r(mean)')/`r(mean)'*100
	
	gen priceratio = (`outputprice'/120)/(`inputprice'/50)
	
	sum priceratio
	gen priceratio_std = (priceratio-`r(mean)')/`r(mean)'*100

	sum travel_adj_urea travel_adj_maize

// generate CDFs		
	local title1 minimum travel-cost adjusted 50kg UREA price (%, mean = 0)
	local title2 maximum travel-cost adjusted 120kg MAIZE price (%, mean = 0)
	local title3 ratio of price per kg between MAIZE and UREA
	
	local filename1 "fertilizerprice"
	local filename2 "maizeprice"
	local filename3 "priceratio"
	
	local i 0
	
	cap graph drop _all
	foreach var in travel_adj_urea travel_adj_maize priceratio{
				   
		local ++i
		cap drop var`i'_cdf
		cumul `var', gen(var`i'_cdf)
		sum `var', d
		di "p10: `r(p10)' and p90: `r(p90)'"
		
		#d;
		scatter var`i'_cdf `var', sort ytitle("% at or below", size(medlarge)) xlabel(#6)
		xtitle("`title`i''", size(medlarge)) xline(`r(p10)', lp(-)) xline(`r(p90)', lp(-))
		graphregion(color(white)) msize(vsmall) name(gcdf`i')
		;
		#d cr
		graph export "${pool_results}/`filename`i''_cdf_censusAVimputed.pdf", replace
		
	}			

	
	
// generate two CDFS with reduced form estimate and model estimate
	local inputprice1 min_DR_adj_price_50kg
	local inputprice2 min_AVE_adj_price_50kg
	
	local title1 minimum travel-cost adjusted 50kg UREA price (%, mean = 0)
	
	cap graph drop _all			   
	cap drop var*_cdf
	
	cap drop var1_cdf var2_cdf
	qui: sum `inputprice1', d
	cumul `inputprice1' if `inputprice1'<=`r(p95)', gen(var1_cdf)
	sum `inputprice1' if `inputprice1'<=`r(p95)', d
	
	qui: sum `inputprice2', d
	cumul `inputprice2' if `inputprice2'<=`r(p95)', gen(var2_cdf)	
	sum `inputprice2' if `inputprice2'<=`r(p95)', d
	
	sum `inputprice1' if var1_cdf>=0.499 & var1_cdf<.501
	local fig1bx : di %6.1f `r(mean)'
	local fig1by = 0.5 
	
	sum `inputprice2' if var2_cdf>=0.499 & var2_cdf<.501
	local fig2bx : di %6.1f `r(mean)'
	local fig2by = 0.5
	
	cap graph drop _all
	#d;
	twoway (scatter var1_cdf `inputprice1' if !mi(var1_cdf), sort msize(tiny)) 
		   (scatter var2_cdf `inputprice2' if !mi(var2_cdf),  sort  msymbol(X) msize(small))
		   (scatteri 0.5 0  `fig2by' `fig2bx' , connect(l) lw(thin) lp(-) lc(black) ms(i))
		   (scatteri 0.5 `fig2bx'  0 `fig2bx' , text(0.025 `=`fig1bx'-1' "`fig1bx'", place(e)) connect(l) color(black) lw(thin) lp(-) lc(black) mlabc(black) ms(i))
		   (scatteri 0.5 `fig1bx'  0 `fig1bx' , text(0.025 `=`fig2bx'-1' "`fig2bx'", place(e)) connect(l) color(black) lw(thin) lp(-) lc(black) mlabc(black) ms(i))
		   
		   
			, 
			ytitle("% at or below", size(medlarge)) xtitle("minimum travel-cost adjusted 50kg UREA price (USD)")
			graphregion(color(white))  name(gcdf_all)
			legend(cols(1) order(1 "Adjusted for pecuniary travel cost" 2 "Adjusted for model estimated iceberg trade cost"))
			;
	#d cr
	
	graph export "${pool_results}/tradecost_cdf2_censusAVimputed.pdf", replace
	
	
	
/******************************************************************************
	CDFs at the nearest locations
*******************************************************************************/
set scheme s2color

// compile the two data sets
	
	// Kilimanjaro
		use "${analysis}/Kilimanjaro ViltoAV_censusappended.dta", clear
		merge 1:1 village_name ward district using "${analysis}/Kilimanjaro ViltoMaizemarket.dta"
		drop _merge
		merge 1:1 village_name ward district using "${analysis}/Kilimanjaro google_ViltoCities.dta"
		drop _merge
		
		gen survey_region="Kilimanjaro"
		
		tempfile kili_vilaccess
		save `kili_vilaccess'
		
	// Manyara
		use "${analysis}/Manyara ViltoAV_censusappended.dta", clear
		merge 1:1 village_id using "${analysis}/Manyara ViltoMaizemarket.dta"
		drop _merge
		merge 1:1 village_id using "${analysis}/Manyara ViltoCities_dist.dta"
		drop _merge
		
	append using `kili_vilaccess'

		
		
// create travel-adjusted prices in relative terms.

	local inputprice min_NEAR_adj_price_50kg
	local outputprice max_NEAR_adj_maize_120kgUSD
	
	foreach var in `inputprice' `outputprice'{
		sum `var', d
		replace `var' = `r(p1)' 	if `var'<`r(p1)' 	& !mi(`var')
		replace `var' = `r(p99)' 	if `var'>`r(p99)' 	& !mi(`var')
	}
		

	sum `inputprice'
	gen travel_adj_urea = (`inputprice'-`r(mean)')/`r(mean)'*100
	
	sum `outputprice'
	gen travel_adj_maize = (`outputprice'-`r(mean)')/`r(mean)'*100
	
	gen priceratio = (`outputprice'/120)/(`inputprice'/50)

	sum travel_adj_urea travel_adj_maize

	
// generate CDFs		
	local title1 minimum travel-cost adjusted 50kg UREA price (%, mean = 0)
	local title2 maximum travel-cost adjusted 120kg MAIZE price (%, mean = 0)
	local title3 ratio of price per kg between MAIZE and UREA
	
	local filename1 "fertilizerprice"
	local filename2 "maizeprice"
	local filename3 "priceratio"
	
	local i 0
	
	cap graph drop _all
	foreach var in travel_adj_urea travel_adj_maize priceratio{
				   
		local ++i
		cap drop var`i'_cdf
		cumul `var', gen(var`i'_cdf)
		sum `var', d
		di "p10: `r(p10)' and p90: `r(p90)'"
		
		#d;
		scatter var`i'_cdf `var', sort ytitle("% at or below", size(medlarge)) xlabel(#8)
		xtitle("`title`i''", size(medlarge)) xline(`r(p10)', lp(-)) xline(`r(p90)', lp(-))
		graphregion(color(white)) msize(vsmall) name(gcdf`i')
		;
		#d cr
		graph export "${pool_results}/`filename`i''_cdf_NEAR.pdf", replace
		
	}			
	
	
	
// generate two CDFS with reduced form estimate and model estimate
	local inputprice1 min_NEAR_adj_price_50kg
	local inputprice2 min_NEAR2_adj_price_50kg
	
	local title1 minimum travel-cost adjusted 50kg UREA price (%, mean = 0)
	
	cap graph drop _all			   
	cap drop var*_cdf
	
	cap drop var1_cdf var2_cdf
	qui: sum `inputprice1', d
	cumul `inputprice1' if `inputprice1'<=`r(p95)', gen(var1_cdf)
	sum `inputprice1' if `inputprice1'<=`r(p95)', d
	
	qui: sum `inputprice2', d
	cumul `inputprice2' if `inputprice2'<=`r(p95)', gen(var2_cdf)	
	sum `inputprice2' if `inputprice2'<=`r(p95)', d
	
	sum `inputprice1' if var1_cdf>=0.499 & var1_cdf<.501
	local fig1bx : di %6.0f `r(mean)'
	local fig1by = 0.5 
	
	sum `inputprice2' if var2_cdf>=0.499 & var2_cdf<.501
	local fig2bx : di %6.0f `r(mean)'
	local fig2by = 0.5
	
	cap graph drop _all
	#d;
	twoway (scatter var1_cdf `inputprice1' if !mi(var1_cdf), sort msize(vsmall) ) 
		   (scatter var2_cdf `inputprice2' if !mi(var2_cdf),  sort msize(vsmall))
		   (scatteri 0.5 0  `fig2by' `fig2bx' , connect(l) lw(thin) lp(-) lc(black) ms(i))
		   (scatteri 0.5 `fig2bx'  0 `fig2bx' , text(0.025 `=`fig1bx'-4' "`fig1bx'", place(e)) connect(l) color(black) lw(thin) lp(-) lc(black) mlabc(black) ms(i))
		   (scatteri 0.5 `fig1bx'  0 `fig1bx' , text(0.025 `=`fig2bx'-4' "`fig2bx'", place(e)) connect(l) color(black) lw(thin) lp(-) lc(black) mlabc(black) ms(i))
		   
		   
			, 
			ytitle("% at or below", size(medlarge)) xtitle("minimum travel-cost adjusted 50kg UREA price (USD)")
			graphregion(color(white))  name(gcdf_all)
			legend(cols(1) order(1 "Adjusted for pecuniary travel cost" 2 "Adjusted for model estimated iceberg trade cost"))
			;
	#d cr
	
	
	
	graph export "${pool_results}/tradecost_cdf2_NEAR.pdf", replace
	
	exit
	

	
		
